package com.sg.java.apps;

import com.sg.java.*;
import com.sg.java.entity.*;
import com.sg.java.util.DateUtils;
import com.sg.java.util.HBaseUtils;
import com.sg.java.util.JedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.sg.java.YHVolt4.readMap;

public class GetDataFromHBase2 {

    private static final Logger log = LoggerFactory.getLogger(GetDataFromHBase2.class);

    public static final SimpleDateFormat yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    static {
//        initReadProperties();
    }

    public static void main(String[] args) throws Exception {
//        final String yearMonthPrefix = args[0];
        String day = args[0];
        int limit = Integer.parseInt(args[1]);
        log.info("day：{}", day);
        log.info("limit：{}", limit);
        String[] DATA_DATES = day.split(",");
//        List<String> DATA_DATES = new ArrayList<>(31);
//        for (int i = 1; i <= 31; i++) {
//            String day = String.valueOf(i);
//            if (day.length() == 1) {
//                day = "0" + day;
//            }
//            DATA_DATES.add(yearMonthPrefix + day);
//        }
//        DATA_DATES.add(day);

        byte[] bytes_info = Bytes.toBytes("info");
        byte[] bytes_U = Bytes.toBytes("U");
        byte[] bytes_METER_ID = Bytes.toBytes("METER_ID");
        byte[] bytes_PHASE_FLAG = Bytes.toBytes("PHASE_FLAG");
        byte[] bytes_DATA_POINT_FLAG = Bytes.toBytes("DATA_POINT_FLAG");
        byte[] bytes_ORG_NO = Bytes.toBytes("ORG_NO");
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
//        final SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");
        final String preSql = "insert into pms3_qkjdy.essential_data_" + day + " (id,sbname,sbid,date_time,dycount,uper_time_array,lower_time_array,valid_date_time,monitor_time,max_value,max_value_time,min_value,min_value_time,device_type,device_level,org_no,org_name,pid,dy_practical_count,pbtype,up_time,down_time,exception_code,voltage_level,avg,serious_high_voltage,grid_type,cons_type,voltage_fluctuation,pbarray,pbcs,high_voltage,low_voltage,severe_high_voltage,severely_low_voltage) values ";
        log.info("preSql：{}", preSql);
        Properties pgProp = PropertiesUtil.createPropertiesFromResource(ResourcePath.dws_business_jdbc_properties);
        String url = pgProp.getProperty("url");
        String user = pgProp.getProperty("user");
        String password = pgProp.getProperty("password");
        Class.forName("org.postgresql.Driver");
        String finalSql = null;
        try (Connection hBaseConn = HBaseUtils.getHBaseConn(PropertiesUtil.createPropertiesFromResource(ResourcePath.hbase_properties));
             Admin admin = hBaseConn.getAdmin();
             java.sql.Connection pgConn = DriverManager.getConnection(url, user, password)) {
            for (String DATA_DATE : DATA_DATES) {
                long s = System.currentTimeMillis();
                Date date = yyyyMMdd.parse(DATA_DATE);
                String tnStr = HBaseNamespace.CQ_HBASE_NAMESPACE + ":cms_volt_curve_" + DATA_DATE;
                TableName tn = TableName.valueOf(tnStr);
                boolean tableAvailable = admin.isTableAvailable(tn);
                if (!tableAvailable) {
                    log.info(tn + "\t表不存在，跳过");
                    continue;
                }
                log.info(tn + "\t表存在，正在执行");
                HTable hTable = (HTable) hBaseConn.getTable(tn);
                int tempReadCount;
                int totalInsertCount = 0;
                Result next = null;
                int round = 0;
//                final Jedis jedis = new Jedis("25.64.41.53", 6379, 100000);
//                jedis.auth("Huawei@169");

                JedisUtils.InitJedisPool("25.64.41.53", 6379, 100000, "Huawei@169");
                Jedis jedis = JedisUtils.getJedis();
                do {
                    long total_s = System.currentTimeMillis();
                    tempReadCount = 0;
                    round++;
                    Scan scan = new Scan();
                    scan.setLimit(limit);
                    scan.addFamily(bytes_info);
                    scan.addFamily(bytes_U);
                    scan.setFilter(new SingleColumnValueFilter(
                            bytes_info, bytes_PHASE_FLAG, CompareOperator.EQUAL, Bytes.toBytes("1")
                    ));
                    if (next != null) {
                        log.info(tn + "\t下次分页数据起始行：{}", Bytes.toString(next.getRow()));
                        scan.withStartRow(next.getRow(), false);
                    }
                    long query_s = System.currentTimeMillis();
                    ResultScanner scanner = hTable.getScanner(scan);
                    long query_e = System.currentTimeMillis() - query_s;
                    long iterate_s = System.currentTimeMillis();
                    List<Result> results = new ArrayList<>(limit);
                    Map<String, Void> METER_IDS = new HashMap<>(limit);
                    for (Result result : scanner) {
                        tempReadCount++;
                        next = result;
                        results.add(result);
                        String METER_ID = Bytes.toString(result.getValue(bytes_info, bytes_METER_ID));
                        METER_IDS.put(METER_ID, null);
                    }

                    readMap(DATA_DATE + YHVolt4.pbgx, DATA_DATE + YHVolt4.yhgx, DATA_DATE + YHVolt4.yhpb, METER_IDS);
//                    final JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "25.64.41.53", 6379, 100000, "Huawei@169");

                    final List<VoltageQualifiedVo> vos = results.stream().map(result -> {
                        CMS_VOLT_CURVE cms_volt_curve = new CMS_VOLT_CURVE();
                        cms_volt_curve.setRow(Bytes.toString(result.getRow()));
                        cms_volt_curve.setMETER_ID(Bytes.toString(result.getValue(bytes_info, bytes_METER_ID)));
                        cms_volt_curve.setDATA_DATE(date);
                        cms_volt_curve.setPHASE_FLAG(Bytes.toString(result.getValue(bytes_info, bytes_PHASE_FLAG)));
                        cms_volt_curve.setDATA_POINT_FLAG(Bytes.toString(result.getValue(bytes_info, bytes_DATA_POINT_FLAG)));
                        cms_volt_curve.setORG_NO(Bytes.toString(result.getValue(bytes_info, bytes_ORG_NO)));
                        result.listCells().stream().filter(cell -> "U".equals(Bytes.toString(CellUtil.cloneFamily(cell)))).forEach(cell -> {
                            try {
                                cms_volt_curve.getCOL_TIME$U().add(new VoltU1_96(Integer.parseInt(
                                        String.valueOf(DateUtils.getPoint(Bytes.toString(CellUtil.cloneQualifier(cell))))),
                                        Bytes.toString(CellUtil.cloneValue(cell))
                                ));
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        });
//                        final Jedis jedis = jedisPool.getResource();
                        VoltageQualifiedVo vo = YHVolt.getResult(cms_volt_curve, DATA_DATE);
//                        jedisPool.returnResource(jedis);
                        return vo;
                    }).filter(Objects::nonNull).collect(Collectors.toList());
//                    jedisPool.close();

                    long iterate_e = System.currentTimeMillis() - iterate_s;
                    if (!vos.isEmpty()) {
                        long insert_s = System.currentTimeMillis();
                        finalSql = preSql + postSql(vos);
//                        finalSql = SqlUtils.newInsertSql(
//                                "pms3_qkjdy.essential_data_t",
//                                vos,
//                                Triple.of("id",VoltageQualifiedVo::getId,null),
//                                Triple.of("sbname",VoltageQualifiedVo::getSbName,null),
//                                Triple.of("sbid",VoltageQualifiedVo::getSbId,null),
//                                Triple.of("date_time",VoltageQualifiedVo::getDateTime,null),
//                                Triple.of("dycount",VoltageQualifiedVo::getDyCount,null),
//                                Triple.of("uper_time_array",VoltageQualifiedVo::getUperTimeArray,null),
//                                Triple.of("lower_time_array",VoltageQualifiedVo::getLowerTimeArray,null),
//                                Triple.of("valid_date_time",VoltageQualifiedVo::getValidDataTime,null),
//                                Triple.of("monitor_time",VoltageQualifiedVo::getMonitorTime,null),
//                                Triple.of("max_value",VoltageQualifiedVo::getMaxValue,null),
//                                Triple.of("max_value_time",VoltageQualifiedVo::getMaxValue,null),
//                                Triple.of("min_value",VoltageQualifiedVo::getMinValue,null),
//                                Triple.of("min_value_time",VoltageQualifiedVo::getMinValueTime,null),
//                                Triple.of("device_type",VoltageQualifiedVo::getDeviceType,null),
//                                Triple.of("device_level",VoltageQualifiedVo::getDeviceLevel,null),
//                                Triple.of("org_no",VoltageQualifiedVo::getOrgNo,null),
//                                Triple.of("org_name",VoltageQualifiedVo::getOrgName,null),
//                                Triple.of("pid",VoltageQualifiedVo::getPid,null),
//                                Triple.of("dy_practical_count",VoltageQualifiedVo::getDyPracticalCount,null),
//                                Triple.of("pbtype",VoltageQualifiedVo::getPbtype,null),
//                                Triple.of("up_time",VoltageQualifiedVo::getUpTime,null),
//                                Triple.of("down_time",VoltageQualifiedVo::getDownTime,null),
//                                Triple.of("exception_code",VoltageQualifiedVo::getExceptionCode,null),
//                                Triple.of("voltage_level",VoltageQualifiedVo::getVoltageLevel,null),
//                                Triple.of("avg",VoltageQualifiedVo::getAvg,null),
//                                Triple.of("serious_high_voltage",VoltageQualifiedVo::getSeriousnessPb,null)
//                        );
                        PreparedStatement ps = pgConn.prepareStatement(finalSql);
                        int insertCount = ps.executeUpdate();
                        totalInsertCount += insertCount;
                        long insert_e = System.currentTimeMillis() - insert_s;
                        log.info(tn + "\t此轮数：{}\t此轮读取总数：{}\t此轮计算插入数：{}\t当前已插入计算总数：{}\t此轮耗时：{}s\t查询hbase耗时：{}ms\t遍历调用redis计算耗时：{}ms\t组装sql入pg库耗时：{}ms", round, tempReadCount, insertCount, totalInsertCount, (System.currentTimeMillis() - total_s) / 1000, query_e, iterate_e, insert_e);
                    }
                } while (tempReadCount >= limit);
                log.info(tn + "\t电压计算完毕\t总轮数：{}\t总计算插入数：{}\t总耗时：{}s", round, totalInsertCount, (System.currentTimeMillis() - s) / 1000);
                hTable.close();
                jedis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("finalSql：{}", finalSql);
            throw new RuntimeException(e);
        }
    }

    public static String postSql(List<VoltageQualifiedVo> vos) {
        StringJoiner postSql = new StringJoiner(",");
        for (VoltageQualifiedVo vo : vos) {
            postSql.add(new StringJoiner(",", "(", ")")
                    .add(vo.getId() != null ? "'" + vo.getId() + "'" : "null")
                    .add(vo.getSbName() != null ? "'" + vo.getSbName() + "'" : "null")
                    .add(vo.getSbId() != null ? "'" + vo.getSbId() + "'" : "null")
                    .add(vo.getDateTime() != null ? "'" + yyyy_MM_dd.format(vo.getDateTime()) + "'" : "null")
                    .add(vo.getDyCount() != null && !Double.isNaN(vo.getDyCount()) ? vo.getDyCount().toString() : "null")
                    .add(vo.getEffectiveDataTime() != null && !Double.isNaN(vo.getEffectiveDataTime()) ? vo.getEffectiveDataTime().toString() : "null")
                    .add(vo.getMonitorTime() != null && !Double.isNaN(vo.getMonitorTime()) ? vo.getMonitorTime().toString() : "null")
                    .add(vo.getMaxValue() != null && !Double.isNaN(vo.getMaxValue()) ? vo.getMaxValue().toString() : "null")
                    .add(vo.getMaxValueTime() != null ? "'" + yyyy_MM_dd_HH_mm_ss.format(vo.getMaxValueTime()) + "'" : "null")
                    .add(vo.getMinValue() != null && !Double.isNaN(vo.getMinValue()) ? vo.getMinValue().toString() : "null")
                    .add(vo.getMinValueTime() != null ? "'" + yyyy_MM_dd_HH_mm_ss.format(vo.getMinValueTime()) + "'" : "null")
                    .add(vo.getDeviceType() != null ? "'" + vo.getDeviceType() + "'" : "null")
                    .add(vo.getDeviceLevel() != null ? "'" + vo.getDeviceLevel() + "'" : "null")
                    .add(vo.getOrgNo() != null ? "'" + vo.getOrgNo() + "'" : "null")
                    .add(vo.getPid() != null ? "'" + vo.getPid() + "'" : "null")
                    .add(vo.getRealVoltagePoint() != null && !Double.isNaN(vo.getRealVoltagePoint()) ? vo.getRealVoltagePoint().toString() : "null")
                    .add(vo.getUpTime() != null && !Double.isNaN(vo.getUpTime()) ? vo.getUpTime().toString() : "null")
                    .add(vo.getDownTime() != null && !Double.isNaN(vo.getDownTime()) ? vo.getDownTime().toString() : "null")
                    .add(vo.getExceptionCode() != null ? "'" + vo.getExceptionCode() + "'" : "null")
                    .add(vo.getVoltageLevel() != null ? "'" + vo.getVoltageLevel() + "'" : "null")
                    .add(vo.getAvg() != null && !Double.isNaN(vo.getAvg()) ? vo.getAvg().toString() : "null")
                    .add(vo.getConsType() != null ? "'" + vo.getConsType() + "'" : "null")
                    .add(vo.getGridType() != null ? "'" + vo.getGridType() + "'" : "null")
                    .add(vo.getHighVoltage() != null ? "'" + vo.getHighVoltage() + "'" : "null")
                    .add(vo.getLowVoltage() != null ? "'" + vo.getLowVoltage() + "'" : "null")
                    .add(vo.getSevereHighVoltage() != null ? "'" + vo.getSevereHighVoltage() + "'" : "null")
                    .add(vo.getSeverelyLowVoltage() != null ? "'" + vo.getSeverelyLowVoltage() + "'" : "null")
                    .add(vo.getHighTime() != null && !Double.isNaN(vo.getHighTime()) ? vo.getHighTime().toString() : null)
                    .add(vo.getLowTime() != null && !Double.isNaN(vo.getLowTime()) ? vo.getLowTime().toString() : null)
                    .add(vo.getSevereHighTime() != null && !Double.isNaN(vo.getSevereHighTime()) ? vo.getSevereHighTime().toString() : null)
                    .add(vo.getSeverelyLowTime() != null && !Double.isNaN(vo.getSeverelyLowTime()) ? vo.getSeverelyLowTime().toString() : null)
                    .add(vo.getHighCs() != null ? vo.getHighCs().toString() : null)
                    .add(vo.getLowCs() != null ? vo.getLowCs().toString() : null)
                    .add(vo.getSevereHighCs() != null ? vo.getSevereHighCs().toString() : null)
                    .add(vo.getSeverelyLowCs() != null ? vo.getSeverelyLowCs().toString() : null)
                    .toString())
            ;
        }
        return postSql.toString();
    }

    public static String postSql2(List<TimeShare> vos) {
        StringJoiner postSql = new StringJoiner(",");
        for (TimeShare vo : vos) {
//            String[] split = vo.split(",");
//            StringJoiner joiner = new StringJoiner(",", "(", ")");
//            for (String s : split) {
//                joiner.add("'" + s + "'");
//            }
//            postSql.add(joiner.toString());
            postSql.add(new StringJoiner(",", "(", ")")
                    .add(vo.getId() !=null ? "'" + vo.getId() + "'" : "null")
                    .add("'" + vo.getU1() + "'")
                    .add("'" + vo.getU2() + "'")
                    .add("'" + vo.getU3() + "'")
                    .add("'" + vo.getU4() + "'")
                    .add("'" + vo.getU5() + "'")
                    .add("'" + vo.getU6() + "'")
                    .add("'" + vo.getU7() + "'")
                    .add("'" + vo.getU8() + "'")
                    .add("'" + vo.getU9() + "'")
                    .add("'" + vo.getU10() + "'")
                    .add("'" + vo.getU11() + "'")
                    .add("'" + vo.getU12() + "'")
                    .add("'" + vo.getU13() + "'")
                    .add("'" + vo.getU14() + "'")
                    .add("'" + vo.getU15() + "'")
                    .add("'" + vo.getU16() + "'")
                    .add("'" + vo.getU17() + "'")
                    .add("'" + vo.getU18() + "'")
                    .add("'" + vo.getU19() + "'")
                    .add("'" + vo.getU20() + "'")
                    .add("'" + vo.getU21() + "'")
                    .add("'" + vo.getU22() + "'")
                    .add("'" + vo.getU23() + "'")
                    .add("'" + vo.getU24() + "'")
                    .add("'" + vo.getU25() + "'")
                    .add("'" + vo.getU26() + "'")
                    .add("'" + vo.getU27() + "'")
                    .add("'" + vo.getU28() + "'")
                    .add("'" + vo.getU29() + "'")
                    .add("'" + vo.getU30() + "'")
                    .add("'" + vo.getU31() + "'")
                    .add("'" + vo.getU32() + "'")
                    .add("'" + vo.getU33() + "'")
                    .add("'" + vo.getU34() + "'")
                    .add("'" + vo.getU35() + "'")
                    .add("'" + vo.getU36() + "'")
                    .add("'" + vo.getU37() + "'")
                    .add("'" + vo.getU38() + "'")
                    .add("'" + vo.getU39() + "'")
                    .add("'" + vo.getU40() + "'")
                    .add("'" + vo.getU41() + "'")
                    .add("'" + vo.getU42() + "'")
                    .add("'" + vo.getU43() + "'")
                    .add("'" + vo.getU44() + "'")
                    .add("'" + vo.getU45() + "'")
                    .add("'" + vo.getU46() + "'")
                    .add("'" + vo.getU47() + "'")
                    .add("'" + vo.getU48() + "'")
                    .add("'" + vo.getU49() + "'")
                    .add("'" + vo.getU50() + "'")
                    .add("'" + vo.getU51() + "'")
                    .add("'" + vo.getU52() + "'")
                    .add("'" + vo.getU53() + "'")
                    .add("'" + vo.getU54() + "'")
                    .add("'" + vo.getU55() + "'")
                    .add("'" + vo.getU56() + "'")
                    .add("'" + vo.getU57() + "'")
                    .add("'" + vo.getU58() + "'")
                    .add("'" + vo.getU59() + "'")
                    .add("'" + vo.getU60() + "'")
                    .add("'" + vo.getU61() + "'")
                    .add("'" + vo.getU62() + "'")
                    .add("'" + vo.getU63() + "'")
                    .add("'" + vo.getU64() + "'")
                    .add("'" + vo.getU65() + "'")
                    .add("'" + vo.getU66() + "'")
                    .add("'" + vo.getU67() + "'")
                    .add("'" + vo.getU68() + "'")
                    .add("'" + vo.getU69() + "'")
                    .add("'" + vo.getU70() + "'")
                    .add("'" + vo.getU71() + "'")
                    .add("'" + vo.getU72() + "'")
                    .add("'" + vo.getU73() + "'")
                    .add("'" + vo.getU74() + "'")
                    .add("'" + vo.getU75() + "'")
                    .add("'" + vo.getU76() + "'")
                    .add("'" + vo.getU77() + "'")
                    .add("'" + vo.getU78() + "'")
                    .add("'" + vo.getU79() + "'")
                    .add("'" + vo.getU80() + "'")
                    .add("'" + vo.getU81() + "'")
                    .add("'" + vo.getU82() + "'")
                    .add("'" + vo.getU83() + "'")
                    .add("'" + vo.getU84() + "'")
                    .add("'" + vo.getU85() + "'")
                    .add("'" + vo.getU86() + "'")
                    .add("'" + vo.getU87() + "'")
                    .add("'" + vo.getU88() + "'")
                    .add("'" + vo.getU89() + "'")
                    .add("'" + vo.getU90() + "'")
                    .add("'" + vo.getU91() + "'")
                    .add("'" + vo.getU92() + "'")
                    .add("'" + vo.getU93() + "'")
                    .add("'" + vo.getU94() + "'")
                    .add("'" + vo.getU95() + "'")
                    .add("'" + vo.getU96() + "'")
                    .add("'" + vo.getStateArea() + "'")
                    .add("'" + vo.getStatisticTime() + "'")
                    .add("'" + vo.getPbId() + "'")
                    .add("'" + vo.getBdzId() + "'")
                    .add("'" + vo.getBdzName() + "'")
                    .add("'" + vo.getXlId() + "'")
                    .add("'" + vo.getXlName() + "'")
                    .add("'" + vo.getMxId() + "'")
                    .add("'" + vo.getPbName() + "'")
                    .toString());
        }
        return postSql.toString();
    }


    public static String postSqlCount(List<VoltageQualifiedVo> vos) {
        StringJoiner postSql = new StringJoiner(",");
        for (VoltageQualifiedVo vo : vos) {
            postSql.add(new StringJoiner(",", "(", ")")
                    .add(vo.getId() != null ? "'" + vo.getId() + "'" : "null")
                    .add(vo.getMonitoringTimeArray() != null ? "'" + vo.getMonitoringTimeArray() + "'" : "null")
                    .toString())
            ;
        }
        return postSql.toString();
    }

    public static ThreadLocal<HashMap<String, Void>> tl = new ThreadLocal<>();

    public static Pair<Integer, String> postLine(List<VoltageQualifiedVo> vos, Map<String, Void> uniqueIds) {
        StringJoiner batchContent = new StringJoiner("\n");
        int count = 0;
        for (VoltageQualifiedVo vo : vos) {
            if (uniqueIds.containsKey(vo.getId())) {
                continue;
            }
            if (StringUtils.isBlank(vo.getOrgNo())) {
                continue;
            }
            String substring = vo.getOrgNo().substring(0, 5);
            if (!(substring.equals("50402") ||
                    substring.equals("50444") ||
                    substring.equals("50405") ||
                    substring.equals("50408") ||
                    substring.equals("50411"))) {
                continue;
            }

            uniqueIds.put(vo.getId(), null);

            count++;
            batchContent.add(new StringJoiner("\t")
                    .add(vo.getId())
                    .add(vo.getSbName())
                    .add(vo.getSbId())
                    .add(vo.getDateTime() != null ? yyyy_MM_dd.format(vo.getDateTime()) : null)
                    .add(vo.getDyCount() != null && !Double.isNaN(vo.getDyCount()) ? vo.getDyCount().toString() : null)
                    .add(vo.getEffectiveDataTime() != null && !Double.isNaN(vo.getEffectiveDataTime()) ? vo.getEffectiveDataTime().toString() : null)
                    .add(vo.getMonitorTime() != null && !Double.isNaN(vo.getMonitorTime()) ? vo.getMonitorTime().toString() : null)
                    .add(vo.getMaxValue() != null && !Double.isNaN(vo.getMaxValue()) ? vo.getMaxValue().toString() : null)
                    .add(vo.getMaxValueTime() != null ? yyyy_MM_dd_HH_mm_ss.format(vo.getMaxValueTime()) : null)
                    .add(vo.getMinValue() != null && !Double.isNaN(vo.getMinValue()) ? vo.getMinValue().toString() : null)
                    .add(vo.getMinValueTime() != null ? yyyy_MM_dd_HH_mm_ss.format(vo.getMinValueTime()) : null)
                    .add(vo.getDeviceType() != null ? vo.getDeviceType() : null)
                    .add(vo.getDeviceLevel() != null ? vo.getDeviceLevel() : null)
                    .add(vo.getOrgNo() != null ? vo.getOrgNo() : null)
                    .add(vo.getPid() != null ? vo.getPid() : null)
                    .add(vo.getRealVoltagePoint() != null && !Double.isNaN(vo.getRealVoltagePoint()) ? vo.getRealVoltagePoint().toString() : null)
                    .add(vo.getUpTime() != null && !Double.isNaN(vo.getUpTime()) ? vo.getUpTime().toString() : null)
                    .add(vo.getDownTime() != null && !Double.isNaN(vo.getDownTime()) ? vo.getDownTime().toString() : null)
                    .add(vo.getExceptionCode() != null ? vo.getExceptionCode() : null)
                    .add(vo.getVoltageLevel() != null ? vo.getVoltageLevel() + "" : null)
                    .add(vo.getAvg() != null && !Double.isNaN(vo.getAvg()) ? vo.getAvg().toString() : null)
                    .add(vo.getConsType() != null ? vo.getConsType() : null)
                    .add(vo.getGridType() != null ? vo.getGridType() : null)
                    .add(vo.getHighVoltage() != null ? vo.getHighVoltage() : null)
                    .add(vo.getLowVoltage() != null ? vo.getLowVoltage() : null)
                    .add(vo.getSevereHighVoltage() != null ? vo.getSevereHighVoltage() : null)
                    .add(vo.getSeverelyLowVoltage() != null ? vo.getSeverelyLowVoltage() : null)
                    .add(vo.getHighTime() != null && !Double.isNaN(vo.getHighTime()) ? vo.getHighTime().toString() : null)
                    .add(vo.getLowTime() != null && !Double.isNaN(vo.getLowTime()) ? vo.getLowTime().toString() : null)
                    .add(vo.getSevereHighTime() != null && !Double.isNaN(vo.getSevereHighTime()) ? vo.getSevereHighTime().toString() : null)
                    .add(vo.getSeverelyLowTime() != null && !Double.isNaN(vo.getSeverelyLowTime()) ? vo.getSeverelyLowTime().toString() : null)
                    .add(vo.getHighCs() != null ? vo.getHighCs().toString() : null)
                    .add(vo.getLowCs() != null ? vo.getLowCs().toString() : null)
                    .add(vo.getSevereHighCs() != null ? vo.getSevereHighCs().toString() : null)
                    .add(vo.getSeverelyLowCs() != null ? vo.getSeverelyLowCs().toString() : null)
                    .toString());
        }
        return Pair.of(count, batchContent.toString());
    }

}
